package cn.wildfire.chat.kit.conversation;

import android.app.Activity;
import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sourcemuch.im.caiba.ManumotiveIBinder;
import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import java.util.List;

import cn.wildfire.chat.app.extendsion.widgets.CircleImageView;
import cn.wildfire.chat.kit.WfcUIKit;
import cn.wildfire.chat.kit.user.UserInfoActivity;
import cn.wildfire.chat.kit.user.UserViewModel;
import cn.wildfirechat.model.GroupMember;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;

/**
 * @Author LiZhong
 * @Date 2019/9/9
 */
public class ViewGroupMemberAdapter extends BaseQuickAdapter<GroupMember, BaseViewHolder> {

    Activity activity;
    String userId;
    GroupMember groupMember;
    List<GroupMember> data;

    public ViewGroupMemberAdapter(Activity activity, int layoutResId, @Nullable List<GroupMember> data, String userId, GroupMember groupMember) {
        super(layoutResId, data);
        this.data = data;
        this.activity = activity;
        this.userId = userId;
        this.groupMember = groupMember;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, GroupMember item) {
        View itemView = helper.itemView;
        CircleImageView headView = helper.getView(R2.id.item_head);
        UserViewModel userViewModel = WfcUIKit.getAppScopeViewModel(UserViewModel.class);
        UserInfo userInfo = userViewModel.getUserInfo(item.memberId, true);

        if (item.lastPosition == 1 || item.lastPosition == 2) {
            if (item.lastPosition == 1) {
                helper.setText(R2.id.item_nick, "");
                Glide.with(activity).load(R.mipmap.icon_delet_member).into(headView);
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (activity instanceof ViewGroupMemberActivity) {
                            ((ViewGroupMemberActivity) activity).onRemoveMemberClick();
                        }
                    }
                });
            } else {
                helper.setText(R2.id.item_nick, "");
                Glide.with(activity).load(R.mipmap.icon_add_member).into(headView);
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (activity instanceof ViewGroupMemberActivity) {
                            ((ViewGroupMemberActivity) activity).onAddMemberClick();
                        }
                    }
                });
            }
        } else {
            UserInfo muserInfo = userViewModel.getUserInfo(item.memberId, groupMember.groupId, true);
            helper.setText(R2.id.item_nick, ManumotiveIBinder.getPreferential(muserInfo));
            Glide.with(activity).load(userInfo.portrait).apply(new RequestOptions().placeholder(R.mipmap.avatar_def).centerCrop()).into(headView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(activity, UserInfoActivity.class);
                    UserInfo muserInfo = userViewModel.getUserInfo(userInfo.uid, groupMember.groupId, true);

                    intent.putExtra("userInfo", muserInfo);
                    intent.putExtra("fromGroupShow", true);
                    intent.putExtra("groupInfo", ChatManager.Instance().getGroupInfo(groupMember.groupId, false));
                    String normalTag = (groupMember.type == GroupMember.GroupMemberType.Manager || groupMember.type == GroupMember.GroupMemberType.Owner) ? "0" : "1";
                    intent.putExtra("normal", normalTag);
                    if (normalTag.equals("1")) { // 普通成员不显示共同群，在群人员列表中进入
                        intent.putExtra("commonGroupLayoutShow", false);
                    }
                    intent.putExtra("privateChat", 1);
                    activity.startActivity(intent);
                }
            });
        }

    }

    /*public void notifyDeleteIcon(){
        if (userId == item.memberId && item.type == GroupMember.GroupMemberType.Normal){
            if (helper.getAdapterPosition() == getData().size() - 1){
                itemView.setVisibility(View.GONE);
            }else{
                itemView.setVisibility(View.VISIBLE);
            }
        }
    }*/

}
